package com.store.controller;

import com.store.entity.Product;
import com.store.service.ProductService;
import com.store.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api("商品控制层")
@RestController
@RequestMapping("/product")
public class ProductController {

    @DubboReference(interfaceClass = ProductService.class,version = "1.0.0",timeout = 5000)
    private ProductService productService;

    @ApiOperation("查询商品")
    @RequestMapping("/getProdoct")
    public JsonResult getProduct(Integer id){
        Product product = productService.getProductById(id);
        return new JsonResult(200,product);
    }

    @ApiOperation("修改商品数量")
    @PostMapping("/modifyProduct")
    public JsonResult modifyProduct(Integer id, HttpSession session){
        String username = (String)session.getAttribute("username");
        productService.updateProductById(id,username);
        return new JsonResult(200);
    }

    @ApiOperation("获取畅销商品")
    @RequestMapping("/getHot")
    public JsonResult getHot(){
        List<Product> productList = productService.getProduct();
        return new JsonResult(200,productList);
    }

    @ApiOperation("获取新到商品")
    @RequestMapping("/getNew")
    public JsonResult getNew(){
        List<Product> productList = productService.getNewProduct();
        return new JsonResult(200,productList);
    }

    @ApiOperation("模糊查询商品")
    @GetMapping("/getSearch")
    public JsonResult getSearch(String name){
        List<Product> productList = productService.getProductBySearch(name);
        return new JsonResult(200,productList);
    }
}
